package com.diudiutechnology.www.yadiudiu.util.view.item;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.view.MyListView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.AllEvaluationActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.GoodsCategoryListAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.GoodsListAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.TotalEvaluationAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.BuyACarFragmentModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.CleanBusiness;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CleanTypeBean;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CleanProductBean;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

import se.emilsjolander.stickylistheaders.ExpandableStickyListHeadersListView;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

/**
 * Created by Administrator on 2017/8/23.
 */

public class DetailsFruitsAndVegetablesView extends LinearLayout implements StickyListHeadersListView.OnHeaderClickListener,
        StickyListHeadersListView.OnStickyHeaderChangedListener,
        StickyListHeadersListView.OnStickyHeaderOffsetChangedListener, GoodsListAdapter.OnShopCartGoodsChangeListener {

    final Context context;
    //商品列表
    MyListView goodsCateGoryList;
    //商品类型
    ExpandableStickyListHeadersListView goodsList;
    private GoodsListAdapter goodsAdapter;//商品列表适配器
    private GoodsCategoryListAdapter goodsCategoryListAdapter;//商品类型列表适配器
    //上一个标题的小标
    private int lastTitlePoi;
    private MyOnGoodsScrollListener myOnGoodsScrollListener;
    //存储含有标题的第一个含有商品类别名称的条目的下表
    private List<Integer> titlePois = new ArrayList<>();
    LinearLayout the_list;//商品
    LinearLayout total_evaluation_layou;//评论
    List<BuyACarFragmentModel> buycarlist;
    TotalEvaluationAdapter adapter;
    ListView releaseListview;
    TextView all_evaluation;
    private String mPos;
    private String mApi;
    private String mProductApi;
    private List<CleanTypeBean> mTypeBean;
    private List<CleanProductBean> mProductBean;

    public DetailsFruitsAndVegetablesView(Context context, String pos) {
        super(context);
        this.context = context;
        this.mPos = pos;
        init();

    }

    public void setData(int pos, Activity mActivity, TextView shopCart) {
        if (pos == 0) {
            the_list.setVisibility(VISIBLE);
            total_evaluation_layou.setVisibility(GONE);
            //  contact.setOnClickListener(this);
            goodsCateGoryList.setDealTouchEvent(true);
            goodsList.setOnHeaderClickListener(this);
            goodsList.setOnStickyHeaderChangedListener(this);
            goodsList.setOnStickyHeaderOffsetChangedListener(this);
            goodsList.setDrawingListUnderStickyHeader(true);
            goodsList.setAreHeadersSticky(true);
            goodsCateGoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    System.out.println("titlePois" + position);
                    goodsList.setSelection(titlePois.get(position));
                }
            });
            fillGoodsList(mActivity, shopCart);
        } else {
            the_list.setVisibility(GONE);
            total_evaluation_layou.setVisibility(VISIBLE);
            adapter = new TotalEvaluationAdapter(context);
            releaseListview.setAdapter(adapter);
            all_evaluation.setText(Html.fromHtml("查看全部评价<font color='#FF4537'>(157)</font>"));
            all_evaluation.setOnClickListener(clickListener);
            initData();
        }
    }

    //获取评论
    private void initData() {
//        buycarlist = new ArrayList<>();
//        BuyACarFragmentModel model = new BuyACarFragmentModel();
//        model.setName("只是想改个名字");
//        model.setStar("2");
//        model.setDistance("");
//        model.setDate("老板服务态度挺好的，洗车也洗得很彻底，里外都收 拾得挺干净的");
//        model.setPraise("2017.05.31");
//        buycarlist.add(model);
//        adapter.addItem(buycarlist);
    }

    /**
     * 填充商品列表
     */
    private void fillGoodsList(Activity mActivity, TextView shopCartNum) {
        getProductTypeData(mActivity, shopCartNum);
    }

    OnClickListener clickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
            context.startActivity(new Intent(context, AllEvaluationActivity.class));
        }
    };

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_details_fruits_and_vegetables, this, true);
        the_list = (LinearLayout) findViewById(R.id.the_list);
        goodsCateGoryList = (MyListView) findViewById(R.id.goodsCateGoryList);
        goodsList = (ExpandableStickyListHeadersListView) findViewById(R.id.goodsList);
        total_evaluation_layou = (LinearLayout) findViewById(R.id.total_evaluation_layou);
        releaseListview = (ListView) findViewById(R.id.release_listview);
        all_evaluation = (TextView) findViewById(R.id.all_evaluation);
    }

    /**
     * 获取干洗分类的数据
     */
    private void getProductTypeData(final Activity mActivity, final TextView shopCartNum) {
        if (mPos.equals("0")) {
            mApi = Constant.CLEAN_TYPE_SHOW;
        } else {
            mApi = Constant.CLEAN_TYPE_SHOW;
        }
        CleanBusiness.postCleanTypeBean(mApi, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                mTypeBean = CleanBusiness.parseToCleanTypeBean(result);
                if (mTypeBean != null && mTypeBean.size() > 0) {
                    goodsCategoryListAdapter = new GoodsCategoryListAdapter(mTypeBean, context);
                    goodsCateGoryList.setAdapter(goodsCategoryListAdapter);
                }
                getProductData(mActivity, shopCartNum);
                myOnGoodsScrollListener = new MyOnGoodsScrollListener();
                goodsList.setOnScrollListener(myOnGoodsScrollListener);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 获取列表的数据
     */
    private void getProductData(final Activity mActivity,final TextView shopCartNum) {
        if (mPos.equals("0")) {
            mProductApi = Constant.CLEAN_DETAIL_SHOW;
        } else {
            mProductApi = Constant.CLEAN_DETAIL_SHOW;
        }
        CleanBusiness.postCleanTypeProductBean(mProductApi, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                mProductBean = CleanBusiness.parseToCleanTypeProductBean(result);
                if (mProductBean != null && mProductBean.size() > 0) {
                    goodsAdapter = new GoodsListAdapter(mProductBean, context, mProductBean);
                    goodsAdapter.setmActivity(mActivity);
                    goodsAdapter.setShopCart(shopCartNum);
                    goodsList.setAdapter(goodsAdapter);
                 //        goodsAdapter.setOnShopCartGoodsChangeListener(this);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onNumChange() {

    }

    @Override
    public void onHeaderClick(StickyListHeadersListView l, View header, int itemPosition, long headerId, boolean currentlySticky) {

    }

    @Override
    public void onStickyHeaderChanged(StickyListHeadersListView l, View header, int itemPosition, long headerId) {

    }

    @Override
    public void onStickyHeaderOffsetChanged(StickyListHeadersListView l, View header, int offset) {

    }

    /**
     * 处理滑动 是两个ListView联动
     */
    private class MyOnGoodsScrollListener implements AbsListView.OnScrollListener {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (!(lastTitlePoi == Integer.valueOf(mProductBean
                    .get(firstVisibleItem)
                    .classid))) {
                lastTitlePoi = Integer.valueOf(mProductBean
                        .get(firstVisibleItem)
                        .classid);
                goodsCategoryListAdapter.setCheckID(Integer.valueOf(mProductBean
                        .get(firstVisibleItem)
                        .classid));
                goodsCategoryListAdapter.notifyDataSetChanged();
            }
        }
    }
}

